package sd.czw.com.mybottomnavigationbar;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseIntArray;
import android.view.MenuItem;
import android.view.MotionEvent;

import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private MyNoScrollViewPager viewPager;
    private BottomNavigationViewEx bottomNavigationViewEx;
    private List<Fragment> fragmentList = new ArrayList<>();
    private ViewPagerAdapter myAdapter;
    private SparseIntArray items;
    public static final String TAG = "CZW";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        items = new SparseIntArray(3);
        initview();
        initfragment();
        initevent();
    }

    private void initevent() {
        bottomNavigationViewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            private int previousPosition = -1;
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = 0;
                switch (item.getItemId()) {
                    case R.id.i_music:
                        id = 0;
                        break;
                    case R.id.i_backup:
                        id = 1;
                        break;
                    case R.id.i_friends:
                        id = 2;
                        break;
                }
                viewPager.setCurrentItem(id);
//                if(previousPosition != id) {
//                  bind.vp.setCurrentItem(id, false);
//                  previousPosition = id;
//                }

//                int position = items.get(item.getItemId());
//                if (previousPosition != position) {
//                    // only set item when item changed
//                    previousPosition = position;
//                    Log.i(TAG, "-----bnve-------- previous item:" + bottomNavigationViewEx.getCurrentItem() + " current item:" + position + " ------------------");
//                    viewPager.setCurrentItem(position);
//                }
                return true;
            }
        });


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bottomNavigationViewEx.setCurrentItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initfragment() {
        MyFragment musicFragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", getString(R.string.music));
        musicFragment.setArguments(bundle);


        MyFragment backupFragment = new MyFragment();
        bundle = new Bundle();
        bundle.putString("title", getString(R.string.backup));
        backupFragment.setArguments(bundle);


        MyFragment friendsFragment = new MyFragment();
        bundle = new Bundle();
        bundle.putString("title", getString(R.string.friends));
        friendsFragment.setArguments(bundle);


        fragmentList.add(musicFragment);
        fragmentList.add(backupFragment);
        fragmentList.add(friendsFragment);

        myAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(myAdapter);
        bottomNavigationViewEx.setupWithViewPager(viewPager);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                viewPager.requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                viewPager.requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.onTouchEvent(event);
    }

    private void initview() {
        viewPager = (MyNoScrollViewPager) findViewById(R.id.viewpager);
        bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bnve);
    }



    class ViewPagerAdapter extends FragmentPagerAdapter{

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

}
